package com.example.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MyBlueTooth extends AppCompatActivity {

    private Button btn_open;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_blue_tooth);

        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();

        btn_open = findViewById(R.id.btn_open);
        btn_open.setOnClickListener(v -> {
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
            }
            defaultAdapter.enable();
            if(defaultAdapter.isEnabled()){
                Toast.makeText(this,"蓝牙已开启",Toast.LENGTH_SHORT).show();
            }else Toast.makeText(this,"蓝牙关闭",Toast.LENGTH_SHORT).show();
        });
    }
}